package com.xu.sms.service.impl;

import com.xu.sms.dao.TypeDao;
import com.xu.sms.entity.Type;
import com.xu.sms.service.TypeService;
import com.xu.sms.utils.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xupenghao@163.com
 * @date 2018-06-05 0:44
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Resource
   private TypeDao typeDao;

    @Override
    @Transactional
    public void add(Type type) {
        typeDao.add(type);
    }

    @Override
    public void delete(int typeId) {

    }

    @Override
    public void update(Type type) {

    }

    @Override
    public List<Type> findAll() {
        return typeDao.findAll();
    }

    @Override
    public Type findTypeById(int typeId) {
        return null;
    }

    @Override
    public List<Type> findTypeByparentId(int parentId) {
        return typeDao.findTypeByparentId(parentId);
    }

    @Override
    public Integer getTotalCounts(Map<String, Object> params) {
        return null;
    }

    @Override
    public List<Type> findAllByPaging(Integer pCur,String typeName,Integer parentId){
        Map<String,Object> params = new HashMap<String,Object>();
        PageResult pr = new PageResult();
        pr.setpStart(pCur);
        params.put("pStart",pr.getpStart());//设置数据库中的起始页
        params.put("pSize",PageResult.PAGERESULT_PSIZE);
        setTypeMapParam(typeName, parentId, params);
        return typeDao.findAllByPaging(params);
    }

    private void setTypeMapParam(String typeName, Integer parentId, Map<String, Object> params) {
        if(!StringUtils.isEmpty(typeName)){
            typeName = "%"+typeName+"%";
        }else{
            typeName =null;
        }
        if(parentId ==null || parentId== -1){
            parentId =null;
        }
        params.put("typeName",typeName);
        params.put("parentId",parentId);
    }

    @Override
    public int getTotalCount(String typeName, Integer parentId) {
        Map<String,Object> params = new HashMap<String,Object>();
        setTypeMapParam(typeName, parentId, params);
        return typeDao.getTotalCounts(params);
    }

    @Override
    public Map<String, Object> findTypeAndParentById(int typeId) {
        return typeDao.findTypeAndParentById(typeId);
    }

    @Override
    public List<Type> findTypeBigCurDate(String curDateStr) {
        return typeDao.findTypeBigCurDate(curDateStr);
    }
}
